package com.djq.hhc.service.impl;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.djq.hhc.dto.UserInfoDto;
import com.djq.hhc.feign.UserInfoFeignClient;
import com.djq.hhc.holder.UserHolder;
import com.djq.hhc.mapper.PostInfoMapper;
import com.djq.hhc.model.post.PostInfo;
import com.djq.hhc.result.Result;
import com.djq.hhc.service.PostInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.djq.hhc.redis.RedisConstants.POST_LIKED_KEY;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: dadd7
 * @Date: 2023/03/02/下午 4:43
 * @Description:
 */

@Service
public class PostInfoServiceImpl extends ServiceImpl<PostInfoMapper, PostInfo> implements PostInfoService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Override
    public Result updateUserById(PostInfo postInfo) {
        boolean b = updateById(postInfo);
        if (b) {
            return Result.ok().message("修改成功");
        } else {
            return Result.ok().message("修改失败");
        }
    }

    @Override
    public Result likePost(Long id) {
        Long userId = UserHolder.getUser().getId();
        if (userId == null) {
            return Result.fail().message("请先登录哟亲亲~");
        }
        String key = POST_LIKED_KEY + id;
        Double score = stringRedisTemplate.opsForZSet().score(key, userId.toString());
        //遇到Boolean,要注意自动拆箱的问题
        if (score == null) {
            boolean update = update().setSql("liked = liked + 1").eq("id", id).update();
            if (update) {
                stringRedisTemplate.opsForZSet().add(key, userId.toString(), System.currentTimeMillis());
                return Result.ok().message("点赞成功");
            }
        } else {
            boolean update = update().setSql("liked = liked - 1").eq("id", id).update();
            if (update) {
                stringRedisTemplate.opsForZSet().remove(key, userId.toString());
                return Result.ok().message("取消点赞");
            }
        }
        return Result.fail().message("点赞出现了错误，请稍后重试");
    }

    //是否点赞
    @Override
    public void isPostLiked(PostInfo postInfo) {
        UserInfoDto user = UserHolder.getUser();
        if (user == null) {
            return;
        }
        Long userId = user.getId();
        String key = POST_LIKED_KEY + postInfo.getId();
        Double score = stringRedisTemplate.opsForZSet().score(key, userId.toString());
        postInfo.setIsLike(score != null);
    }

    @Override
    public Result getLikesRank(String token, Long id) {
        String key = POST_LIKED_KEY + id;
        //查询前五点赞
        Set<String> top5 = stringRedisTemplate.opsForZSet().range(key, 0, 4);
        //既要判断null，又要判断空
        if (top5 == null || top5.isEmpty()) {
            return Result.ok(Collections.emptyList());
        }
        Result result = userInfoFeignClient.getDtosBySet(token, top5);
        Object data = result.getData();
        return Result.ok(data);
    }
}
